package com.ada.tft_sport.fragment;

import android.app.backup.BackupManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ada.tft_sport.MainActivity;
import com.ada.tft_sport.R;

import java.util.Locale;

public class LanguageFragment extends Fragment implements OnClickListener{
	private MainActivity l_mainActivity = (MainActivity)getActivity();
	
	private View languageFragment; // 主界面
	private ImageView iv_language1,iv_language2;
	private FragmentManager fManager; // 碎片管理器
	private FragmentTransaction fTransaction; // 获取事务处理对象
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 每次切换此页面时，不需要重新创建Fragment对象
		setRetainInstance(true);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		// 实例化布局文件
		languageFragment = inflater.inflate(R.layout.fragment_language, container,
				false);

		fManager = getActivity().getSupportFragmentManager(); 	// 获得碎片管理器
		fTransaction = fManager.beginTransaction(); 			// 获取事务处理对象
		
		initLanguage();
		//
		
		return languageFragment; // 返回视图对象
		
	}

	private void initLanguage() {
		// TODO Auto-generated method stub
		iv_language1=(ImageView)languageFragment.findViewById(R.id.language01);
		iv_language2=(ImageView)languageFragment.findViewById(R.id.language02);
		iv_language1.setOnClickListener(this);
		iv_language2.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
		Resources resources =getResources();//获得res资源对象  
		Configuration config = resources.getConfiguration();//获得设置对象  		
		DisplayMetrics dm = resources.getDisplayMetrics();//获得屏幕参数：主要是分辨率，像素等
		Intent intent = new Intent();
		
		switch(arg0.getId()){
		case R.id.language01:
			config.locale = Locale.SIMPLIFIED_CHINESE;
			
    		intent.setClass(getActivity(), MainActivity.class);
    		startActivity(intent);
			BackupManager.dataChanged("com.android.providers.settings");
			
			break;
        case R.id.language02:       	
    		config.locale = Locale.ENGLISH;
    	//	l_mainActivity.finish();
    		
    		intent.setClass(getActivity(), MainActivity.class);
    		startActivity(intent);
    		BackupManager.dataChanged("com.android.providers.settings");
    		
			break;
		default:
			config.locale=Locale.getDefault();
			break;
		}
		resources.updateConfiguration(config, dm);
	}



}
